import Web3 from 'web3'
import { ElMessage } from 'element-plus'

//方法体
function show(){
    console.log("我是测试js");
}

function sliceAdd(add){
    if(add && add.length > 10){
        return add.slice(0,6) + '…'+ add.substring(add.length-4,add.length)
    }else{
        return ''
    }
}

async function loadWeb3 (){
    let web3js;
    if (window.ethereum) {
        web3js = new Web3(window.ethereum)
        // try {
        //   const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });
        //   console.log(accounts);
        // } catch (error) {
        //   if (error.code === 4001) {
        //     // User rejected request
        //   }
        //   ElMessage.error(error)
        // }
        window.web3js = web3js
        // console.log(window.web3js)
        return web3js
    } else {
        web3js = new Web3("https://ropsten.infura.io/v3/54828bfa2cb14873a43512fd8d3fc24b");
        // console.log(web3js)
        // console.log('web3js--')
        window.web3js = web3js
        return web3js
    }
}

// usdt合约地址
function loadContract(abi,usdt){
    // console.log(abi,usdt)
    // console.log(window.web3js)
    if(window.web3js){
        return new window.web3js.eth.Contract(abi,usdt);
    }
    return false
    
}

// 获取账户
async function getAccount(address){
    if(address){
        return address
    }else{
        try {
          const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });
        //   console.log(accounts[0]);
        //   useStore().commit('setAddress',accounts[0])
        //   window.accounts = accounts[0]
          return accounts[0]
        } catch (error) {
          ElMessage.error(error.message)
          return false
        }
    }
}
// 签名
const Tx=(fun,address,value,succeed,error)=>{  
    try {
        fun.send({ from: address , value: value}, 
            (hash) => {
                //  debugger
                console.log(hash)
                if (hash && hash.length > 0) {
                    const status_checker = setInterval(async () => {
                        const tx = await window.web3js.eth.getTransaction(hash)
                        // console.log({ getTransaction: tx })
                        if (!(typeof tx.blockHash == 'object' && tx.blockHash === null)) {
                            const txrt = await window.web3js.eth.getTransactionReceipt(hash)
                            console.log({ getTransactionReceipt: txrt })
                            clearInterval(status_checker)
                            switch (txrt.status) {
                                case true:
                                    console.log('--ok--')
                                    break
                                default:
                                    console.log('--err--')
                                    break
                            }
                        }
                    }, 1000)
                }
        }).then(
            (res) => {
                if (res.status === true) {
                    succeed && succeed(res)
                } else {
                    error && error(res)
                }
            },
            (failure) => {
                // console('failure:'+failure.code+failure.message) //code: 4001, message: user cancelled
                console.log('failure')
                error && error(failure.message)
            }
        )
    } catch (err) {
        console.log('catch')
        error && error(err.message)
    }
    
}



//导出
export default{
   show,
   loadWeb3,
   loadContract,
   getAccount,
   sliceAdd,
   Tx,
}